package hehe.log.util;

import java.io.*;

/**
 * 文件工具类
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class FileUtil
{

    /**
     * 创建文件写操作句柄
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static  Writer createWriter(String filename,boolean fileAppend,String encoding) throws IOException
    {

        FileOutputStream ostream = null;
        try {
            ostream = new FileOutputStream(filename, fileAppend);
        } catch (FileNotFoundException var9) {
            // 文件不存在,则创建
            label29: {
                String parentName = (new File(filename)).getParent();
                if(parentName != null) {
                    File parentDir = new File(parentName);
                    if(!parentDir.exists() && parentDir.mkdirs()) {
                        ostream = new FileOutputStream(filename, fileAppend);
                        break label29;
                    }

                    throw var9;
                }

                throw var9;
            }
        }

        // 字符流转字节流
        Object fw = toOutputStreamWriter(ostream,encoding);

        return (Writer)fw;
    }

    /**
     * 字符流转字节流
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    public static OutputStreamWriter toOutputStreamWriter(OutputStream os, String encoding)
    {
        OutputStreamWriter writer = null;
        if (encoding != null) {
            try {
                writer = new OutputStreamWriter(os, encoding);
            } catch (IOException var5) {
                if(var5 instanceof InterruptedIOException) {
                    Thread.currentThread().interrupt();
                }
            }
        }

        if (writer == null) {
            writer = new OutputStreamWriter(os);
        }

        return writer;
    }

    public static void clodeWriter(Writer writer) throws IOException
    {
        writer.close();
    }

}
